﻿#include "assemblelog.h"
#include "ui_assemblelog.h"

AssembleLog::AssembleLog(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::AssembleLog)
{
    ui->setupUi(this);

    table=TD_ASSEMBLE_LOG;

    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);
    ui->listView->setEditTriggers(QListView::NoEditTriggers);

    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QTableView::DoubleClicked);

    updateMonth();
    updateDate();
    createMenu();
    // updateView(); 不常用，无需更新
}

AssembleLog::~AssembleLog()
{
    delete ui;
}

void AssembleLog::generateMenu()
{
    menu->addAction(QStringLiteral("新记录 (F1)"),this,&AssembleLog::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新日期 (F4)"),this,&AssembleLog::updateMonth);
    menu->addAction(QStringLiteral("更新视图 (F5)"),this,&AssembleLog::updateView);
    menu->addSeparator();

    QMenu *m_option=menu->addMenu(QStringLiteral("选项"));
    m_option->addAction(QStringLiteral("生成记录"),this,&AssembleLog::generateSummary);
    m_option->addSeparator();
    m_option->addAction(QStringLiteral("删除 (Delete)"),this,&AssembleLog::removeItem);

    menu->addSeparator();
    menu->addAction(QStringLiteral("撤销 (Ctrl+Z)"),this,&AssembleLog::revert);
    menu->addAction(QStringLiteral("保存 (Ctrl+S)"),this,&AssembleLog::save);
}

void AssembleLog::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S)
        save();
    else if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Z)
        revert();
    else
    {
        switch (event->key()) {
        case Qt::Key_F1:
            newItem();
            break;
        case Qt::Key_F4:
            updateDate();
            break;
        case Qt::Key_F5:
            updateView();
            break;
        case Qt::Key_Delete:
            removeItem();
            break;
        }
    }
}

void AssembleLog::newItem()
{
    AL_AddItem itemAdd(this);
    connect(&itemAdd,&AL_AddItem::accepted,this,&AssembleLog::updateView);
    if(itemAdd.exec() == QDialog::Accepted)
        updateDate();
}

void AssembleLog::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(index.isValid())
        model->removeRow(index.row());
}

void AssembleLog::updateMonth()
{
    if(ui->month->count() != 0)
        ui->month->clear();

    QStringList items=sp_getCategory("month_",table);
    if(!items.isEmpty())
    {
        ui->month->addItems(items);
        ui->month->setCurrentIndex(items.size() - 1);
    }
}

void AssembleLog::updateDate()
{
    if(selectMonth.isEmpty())
        selectMonth=CurrentMonth;

    if(listModel->rowCount() != 0)
        listModel->setStringList(QStringList());

    QStringList items=sp_getCategory("date_",table,"month_",selectMonth);
    if(!items.isEmpty())
        listModel->setStringList(sp_reverseList(items));
}

void AssembleLog::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectDate=index.data().toString();
        updateView();
    }
}

void AssembleLog::updateView()
{
    if(selectDate.isEmpty())
        selectDate=Today;

    model->setTable(table);
    model->setFilter(QString("date_='%1'").arg(selectDate));
    model->select();

    ui->tableView->hideColumn(0);   //id
    ui->tableView->hideColumn(1);   //month_
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("日期"));    //date_
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("成员姓名"));    //provider
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("产品名称"));    //name
    ui->tableView->setColumnWidth(4,240);
    model->setHeaderData(5,Qt::Horizontal,QStringLiteral("数量"));    //number
    model->setHeaderData(6,Qt::Horizontal,QStringLiteral("备注"));    //remark
}

void AssembleLog::on_month_currentTextChanged(const QString &text)
{
    selectMonth=text;
    updateDate();
}

void AssembleLog::generateSummary()
{
    if(selectDate.isEmpty())
    {
        qApp->clipboard()->setText(QStringLiteral("未选择日期！"));
        return;
    }

    QSqlQuery query;
    query.exec(QString("SELECT name,number FROM %1 WHERE date_='%2'")
               .arg(table)
               .arg(selectDate));
    QStringList items;
    items.append(selectDate + QStringLiteral(" 箭生产记录："));
    while(query.next())
    {
        QString name=query.value(0).toString();
        if(name.isEmpty())
            continue;

        int i_number=query.value(1).toInt();
        if(i_number == 0)
            continue;

        QString number=QString::number(i_number);
        QString item=name + QString(" ") + number;
        items.append(item);
    }

    if(!items.isEmpty())
    {
        QString summary=items.join("\n");
        qApp->clipboard()->setText(summary);
    }
    else
        qApp->clipboard()->setText(QStringLiteral("今日无记录！"));
}
